/********************************************************************************
Discrimination in Multi-Phase Systems: Evidence from Child Protection

Created on: 1/28/2022
Last Modified on: 12/16/2023

Description: This programs generates the moments needed to estimate the HMTE 
model in our paper. 

Note that we have removed the file directory names from this program for 
confidentiality reasons.
********************************************************************************/

**************************
**(0) SETUP
**************************
clear
set more off
macro drop all
capture log close

*Set directories 
global cleandata 
global tmpdata 
global matdata 

**************************
**(1) GENERATE MOMENTS FOR MATLAB ESTIMATION 
**************************
//Start with regression-adjusted investigator placement and maltreatment rates,
//where re-investigation within six months is the outcome//
use "${cleandata}limited_inv_analysis_sample.dta" , clear 

*Rename variables as in ADH (AER, 2022) to make code consistent and easy to adapt 
	rename (w_fe_inv6m b_fe_inv6m w_fe_fc b_fe_fc worker_id count_inv w_se_fe_inv6m b_se_fe_inv6m w_se_fe_fc b_se_fe_fc) ///
	(w_fe_any_misconduct b_fe_any_misconduct w_fe_trial_released b_fe_trial_released ///
	judgeid count_judge w_se_fe_any_misconduct b_se_fe_any_misconduct w_se_fe_trial_released b_se_fe_trial_released)

	gsort judgeid
	gen judgeid_seq = _n 
	
*Gen crosswalk of investigators to preserve confidentiality 
 	preserve 
	keep judgeid judgeid_seq
	export delimited using "${matdata}judgeid_xwalk_qje.csv", replace
 	restore, preserve
	
*Generate remaining variables and export as CSVs (these will then be inputs to the Matlab programs)
	keep judgeid_seq b_fe_any_misconduct b_se_fe_any_misconduct b_fe_trial_released b_se_fe_trial_released ///
	w_fe_any_misconduct w_se_fe_any_misconduct w_fe_trial_released w_se_fe_trial_released
foreach var of varlist *_se_* {
	gen `var'2=`var'^2
}
foreach r in b w {
	export delimited `r'_fe_trial_released `r'_fe_any_misconduct using "${matdata}`r'_ests_qje.csv", replace 
	export delimited `r'_se_fe_trial_released2 `r'_se_fe_any_misconduct2 using "${matdata}`r'_var_qje.csv", replace 
}
		restore, preserve  
	
*Generate "number of cases" per "investigator"
keep judgeid_seq count_judge
export delimited using "${matdata}count_judge_qje.csv", replace
